package com.web.mqtt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.web.common.datasource.DataSourcesSymbol;
import com.web.common.iot.mqtt.entity.MqttAcl;
import com.web.common.iot.mqtt.entity.MqttAclEntity;
import com.web.common.iot.mqtt.entity.MqttUser;
import com.web.mqtt.mapper.MqttAclMapper;
import com.web.mqtt.mapper.MqttUserMapper;
import com.web.service.handler.auth.UserUtil;
import com.web.service.handler.error.CustomException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.web.common.result.R;
import com.web.common.result.ResultEnum;
import com.web.common.user.entity.WebUser;

/**
 * <p>
 * mqtt授权 服务实现类
 * </p>
 *
 * @author zhouhui
 * @since 2024-04-25
 */
@Service
public class MqttAclServiceImpl extends ServiceImpl<MqttAclMapper, MqttAcl> {
	
	@Autowired
	private MqttUserMapper mqttUserMapper;

	/**
	 * 根据条件获取MQTT的权限内容
	 * @param condition 查询条件
	 * @return R<IPage<MqttAclEntity>> 数据列表
	 *
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	public R<IPage<MqttAclEntity>> getList(MqttAclEntity condition) {
		setSearchCondition(condition);
		
		Page<MqttAcl> page = new Page<>(condition.getPage(), condition.getSize());
		IPage<MqttAclEntity> dataList = this.baseMapper.getMqttAclList(page, condition);
		return R.ok(dataList);
	}
	
	/**
	 * 设置查询条件
	 * @param condition 查询条件
	 * 
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	private void setSearchCondition(MqttAclEntity condition) {
		if(StringUtils.hasText(condition.getClientId())) {
			condition.setClientId("%" + condition.getClientId() + "%");
		}
		if(StringUtils.hasText(condition.getUsername())) {
			condition.setUsername("%" + condition.getUsername() + "%");
		}
		if(StringUtils.hasText(condition.getTopic())) {
			condition.setTopic("%" + condition.getTopic() + "%");
		}
	}
	
	/**
	 * 保存数据信息。
	 * 注：新增、修改时就不进行重复项的处理了，因为不影响后续topic的发布和订阅！
	 * @param mqttAcl 数据信息
	 * @return R<Object> 处理结果
	 *
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	public R<Object> saveInfo(MqttAcl mqttAcl) {
		verifyMqttAclParam(mqttAcl);
		
		WebUser user = UserUtil.getUser();
		mqttAcl.setCreateUserId(user.getUserId());
		mqttAcl.setCreateTime(LocalDateTime.now());
		int row = this.baseMapper.insert(mqttAcl);
		if(row <= 0) {
			throw new CustomException(ResultEnum.DATA_INSERT_ERROR.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 校验MQTT ACL的参数
	 * @param mqttAcl 参数信息
	 *
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	private void verifyMqttAclParam(MqttAcl mqttAcl) {
		if(mqttAcl.getType() == 1 && !StringUtils.hasText(mqttAcl.getClientId())) {
			throw new CustomException("未填写客户端Id");
		}
		if(mqttAcl.getType() == 2 && !StringUtils.hasText(mqttAcl.getUsername())) {
			throw new CustomException("未填写用户名");
		}
		
		if(StringUtils.hasText(mqttAcl.getClientId()) || StringUtils.hasText(mqttAcl.getUsername())) {
			LambdaQueryWrapper<MqttUser> query = new LambdaQueryWrapper<>();
			if(StringUtils.hasText(mqttAcl.getClientId())) {
				query.eq(MqttUser::getClientId, mqttAcl.getClientId());
			}
			if(StringUtils.hasText(mqttAcl.getUsername())) {
				query.eq(MqttUser::getUsername, mqttAcl.getUsername());
			}
			List<MqttUser> mqttUserList = mqttUserMapper.selectList(query);
			if(mqttUserList.isEmpty()) {
				throw new CustomException("未查询到客户端信息");
			}
		}
	}
	
	/**
	 * 更新ACL 数据信息
	 * @param mqttAcl 更新的信息
	 * @return R<Object> 处理结果
	 *
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	public R<Object> updateInfo(MqttAcl mqttAcl) {
		verifyMqttAclParam(mqttAcl);
		
		WebUser user = UserUtil.getUser();
		mqttAcl.setUpdateUserId(user.getUserId());
		mqttAcl.setUpdateTime(LocalDateTime.now());
		LambdaUpdateWrapper<MqttAcl> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(MqttAcl::getDelFlag, DataSourcesSymbol.DEL_FLAG_VALUE_0);
		updateWrapper.eq(MqttAcl::getMainId, mqttAcl.getMainId());
		int row = this.baseMapper.update(mqttAcl, updateWrapper);
		if(row <= 0) {
			throw new CustomException(ResultEnum.DATA_UPDATE_ERROR.getMessage());
		}
		
		return R.ok();
	}
	
	/**
	 * 删除MQTT ACL信息
	 * @param mqttAcl 需要删除的ACL
	 * @return R<Object> 处理结果
	 *
	 * @author zhouhui
	 * @since 2024.04.25 
	 */
	public R<Object> deleteInfo(MqttAcl mqttAcl) {
		LambdaUpdateWrapper<MqttAcl> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(MqttAcl::getDelFlag, DataSourcesSymbol.DEL_FLAG_VALUE_0);
		updateWrapper.eq(MqttAcl::getMainId, mqttAcl.getMainId());
		updateWrapper.set(MqttAcl::getDelFlag, DataSourcesSymbol.DEL_FLAG_VALUE_1);
		int row = this.baseMapper.update(mqttAcl, updateWrapper);
		if(row <= 0) {
			throw new CustomException(ResultEnum.DATA_DELETE_ERROR.getMessage());
		}
		return R.ok();
	}
}
